package dp.danhngon.danhngoncuocsong;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.AvoidXfermode;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import dp.danhngon.danhngoncuocsong.R;

public class DanhNgonCuocSongActivity extends Activity {
	/** Called when the activity is first created. */
	public static final String DB_NAME="danhngon.db";
	public static final String TB_NAME="chuong";
	private SQLiteDatabase sql;
	private Cursor cusor;
	// tesss
	public String fileName, strTitle;
	String str = "", str1 = "";
	TextView txtAppTitle;
	Button btAbout;
	private Dialog dialog;
	ArrayList<String[]> listChapter = new ArrayList<String[]>();
	//so file truyen vao		
	/*
	 * sua n, n bang so file trong mang arrFile
	 * */
	int n=4;
	//ten title hien thi tren dau`	
	private String[] arrFileName=new String[n];
	//ten file trong drawable
	
	/*
	 * sua ten file truyen vao
	 * */
	private String[] arrFile={"tinhyeu","tinhban","me","loinoivacuocsong","nguoithay","phunu","longkiennhan","doinhanxuthe"};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
		
		 
		
		txtAppTitle=(TextView)findViewById(R.id.txtAppTitle);
		//txtAppTitle.setText();
		//mang lay ten tieu de
		arrFileName=getResources().getStringArray(R.array.arr_tb_content);
		//mang lay ten file
		//arrFile={"a","b","c"};
		
		btAbout=(Button)findViewById(R.id.btAbout);
		btAbout.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				dialog = new Dialog(DanhNgonCuocSongActivity.this);
		        dialog.setContentView(R.layout.about);
		        //dialog.setTitle("This is my custom dialog box");
		        dialog.setCancelable(true);
		        
		        Button button = (Button) dialog.findViewById(R.id.button1);
		        button.setOnClickListener(new OnClickListener() {
		        public void onClick(View v) {
		               dialog.dismiss();
		            }
		        });
		        //now that the dialog is set up, it's time to show it    
		        dialog.show();
				
			}
		});
		//ArrayList<ItemDetails> item_details = getTieude();
		ArrayList<ItemDetails> item_details = GetSearchResults();

		final ListView lv1 = (ListView) findViewById(R.id.listV_main);
		lv1.setAdapter(new ItemListBaseAdapter(this, item_details));



		lv1.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Object o = lv1.getItemAtPosition(position);
				ItemDetails obj_itemDetails = (ItemDetails) o;
			
				for(int i=0;i<arrFileName.length;i++){
					if (obj_itemDetails.getName() == arrFileName[i]) {
						
						categoriesDetail(arrFile[i],arrFileName[i]);					
					}
					
				}

			}
		});
		lv1.setOnItemLongClickListener(new OnItemLongClickListener() {

			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				return false;
			}
		});
	}
	/*
	public ArrayList<ItemDetails> getTieude(){		
		ArrayList<ItemDetails> arrTieude=new ArrayList<ItemDetails>();
		ItemDetails it;
		cusor = sql.query("chuong", null, null, null, null, null, null);
        if(cusor!=null){
        	cusor.moveToFirst();
        	while(!cusor.isAfterLast()){
        		it=new ItemDetails();
        		it.setName(cusor.getString(1));
        		it.setCount(String.valueOf(cusor.getInt(2)));
        		arrTieude.add(it);           	
            	cusor.moveToNext();
            }
            cusor.close();
        }
        return arrTieude;
	}
	
	public ArrayList<String> getSocau(){
		StringBuilder tieude = new StringBuilder();
		ArrayList<String> arrSocau=new ArrayList<String>();
		cusor = sql.query("chuong", null, null, null, null, null, null);
        if(cusor!=null){
        	cusor.moveToFirst();
        	while(!cusor.isAfterLast()){
            	//tieude.append(cusor.getString(1)+"|"+"\n");
            	arrSocau.add(String.valueOf(cusor.getInt(2)));
            	//arrTieude.add(cusor.getString(2));
            	cusor.moveToNext();
            }
            cusor.close();
        }
        return arrSocau;
	}
	*/
	public void setTitle(String fileName){
		
	}
	public void categoriesDetail(String fileName, String strTitle){
		// Tao intent truyen list la danh sach cac doan vao lop SubListView
		//String fileName="noidung";
		try {												
			Bundle myBundle = new Bundle();
			myBundle.putString("param", fileName);	
			myBundle.putString("title", strTitle);
			Intent subListIntent = new Intent(DanhNgonCuocSongActivity.this, SubListView.class);
			subListIntent.putExtras(myBundle);
			//myBundle.putStringArrayList("loveQuotations",
				//	listChapter);
			startActivityForResult(subListIntent, 0);
			//startActivity(subListIntent);
		} 
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private ArrayList<ItemDetails> GetSearchResults() {
				
		ArrayList<ItemDetails> results = new ArrayList<ItemDetails>();
		//ItemDetails item_details = new ItemDetails();
		
		ItemDetails item_details;
		int count;
		String strCount;
		try{
			for(int i=0;i<arrFile.length;i++){
				listChapter=PlayWithRawFile(arrFile[i]);
				count=listChapter.size();
				strCount=String.valueOf(count);
				//add vao adapter
				item_details=new ItemDetails();
				item_details.setName(arrFileName[i]);
				item_details.setCount(strCount);
				results.add(item_details);
			}
			
		}
		catch (Exception e) {
			e.printStackTrace();
		}		
		
		return results;
	}

	// xay dung ham PlayWithRawFile(fileName) truyen vao ten file va tra ve list
	// gom cac doan trong file
	public ArrayList< String[]> PlayWithRawFile(String fileName) throws IOException {
		ArrayList<String[]> listChapter = new ArrayList<String[]>();

		StringBuffer buf = new StringBuffer();
		InputStream is = this.getResources().openRawResource(
				getResources().getIdentifier(fileName, "drawable",getPackageName()));
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		// read file
		if (is != null) {
			// Doc tung doan va add vao list
			while ((str = reader.readLine()) != null) {
				//tach phan noi dung va tac gia
			 	String[] arrQuotation=str.split(";");
				listChapter.add(arrQuotation);
			}
		}
		is.close();
		return listChapter;
	}
	//doc file lay ve cac doan
	public ArrayList<String> getParagraph(String param) throws IOException{

    	ArrayList<String> listChapter= new ArrayList<String>();
    	
    	StringBuffer buf = new StringBuffer();			   	
    	InputStream is = this.getResources().openRawResource(getResources().
    			getIdentifier(param,"drawable", getPackageName()));
    	
    	BufferedReader reader = new BufferedReader(new InputStreamReader(is));    	
    	//read file
    	if (is!=null) {		
    		//Doc tung doan va add vao list
    		while ((str = reader.readLine()) != null) {	
    			listChapter.add(str);    			
    		}			
    	}		
    	is.close();	

    	return listChapter;
    }
	

}